package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.HttpRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * 检查相管理
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference //远程查找
    private MemberService memberService;


    //新增检查项
    @RequestMapping("/add")
    public Result add(@RequestBody Member member){
        try{
            MemberService.add(member);
        }catch(Exception e){
          e.printStackTrace();
          //如果失败 则返回失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    //检查项查询 分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = memberService.findPage(queryPageBean);
        return pageResult;
    }
    //按ID进行删除
    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            memberService.deleteById(id);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //修改
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member){
        try{
            memberService.edit(member);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }


    //按ID查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
         Member member =memberService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,member);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //查询检查项
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Member> list=memberService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch(Exception e){
            e.printStackTrace();
            //如果失败 则返回失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
        @RequestMapping("/excelReport")
    public Result exportReportExcel(HttpServletRequest request, HttpServletResponse response){
        try {
            Map<String,Object> result=memberService.getexportReportExcel();
            String name = (String) result.get("name");
            String sex= (String) result.get("sex");
            String phoneNumber= (String) result.get("phoneNumber");
            String address= (String) result.get("address");
            String seatmeal= (String) result.get("seatmeal");
            List<CheckGroup> checkgroup= (List<CheckGroup>) result.get("checkgroup");
            List<CheckItem> checkitem= (List<CheckItem>) result.get("checkitem");

          String filePath=  request.getSession().getServletContext().getRealPath("template")+ File.separator+"member.xlsx";

          //基于提供的Excel模板文件在内存中创建一个Excel表哥对象
            XSSFWorkbook excel=new XSSFWorkbook(new FileInputStream(new File(filePath)));
            //读取第一个工作表
            XSSFSheet sheet=excel.getSheetAt(0);
            XSSFRow row=sheet.getRow(2);
            row.getCell(2).setCellValue(name);
            row=sheet.getRow(3);
            row.getCell(3).setCellValue(sex);
            row=sheet.getRow(4);
            row.getCell(4).setCellValue(phoneNumber);
            row=sheet.getRow(5);
            row.getCell(5).setCellValue(address);
            row=sheet.getRow(6);
            row.getCell(6).setCellValue(seatmeal);
//            row=sheet.getRow(7);
//            row.getCell(7).setCellValue(checkgroup);
//            row=sheet.getRow(8);
//            row.getCell(8).setCellValue(checkitem);

            //使用输出流进行表格下载，基于浏览器作为客户端下载
            OutputStream out=response.getOutputStream();
            //代表的是Excel文件类型
            response.setContentType("application/vnd.ms-excel");
            //指定以附件形式进行下载
            response.setHeader("content-Disposition","attachment;filename=member.xlsx");
            excel.write(out);

            out.flush();
            out.close();
            excel.close();
            return null;

        }catch (Exception e){
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);

        }

        }
}
